<script src="__STATICADMIN__layui/layui.js"></script>
<script src="__STATICADMIN__js/common.js"></script>

<!-- // 全局加载第三方JS -->
<!-- <script src="__STATICADMIN__js/cascadata.js"></script> -->
<script src="/static/js/tinymce/tinymce.min.js"></script>
<!-- <script src="__STATICADMIN__module/xmselect/xmselect.js"></script> -->
<!-- // 加载font-awesome图标 -->
<!-- <link href="__STATICADMIN__layui/css/font-awesome.css" rel="stylesheet" type="text/css" /> -->
<!-- <script src="/static/js/markdown/cherry-markdown.min.js"></script>
<link href="/static/js/markdown/cherry-markdown.css" rel="stylesheet" type="text/css" /> -->
<script>
    layui.use(['form', 'layer'], function () {
        $ = layui.jquery;
        var form = layui.form
            , layer = layui.layer;
        //监听省份选择
        form.on('select(province)', function (data) {
            if (!data.value) {
                $('select[name="city"]').html('');
                $('select[name="area"]').html('');
                $('select[name="street"]').html('');
                form.render();
                return false;
            }
            //发异步，把数据提交给php
            $.ajax({
                type: 'get',
                url: '/base/getregion',
                data: { area_code: data.value, level: 2 },
                success: function (res) {
                    if (res.code == 200) {
                        var str = '<option value="">请选择城市</option>';
                        $.each(res.data, function (i, obj) {
                            str += '<option value="' + obj.area_code;
                            str += '">' + obj.name;
                            str += '</option>';
                        });
                        $('select[name="city"]').html(str);
                        $('select[name="area"]').html('');
                        $('select[name="street"]').html('');
                        form.render();
                    } else {
                        layer.msg(res.msg, { icon: 2, time: 1000 });
                    }
                }
            });
            return false;
        });
        //监听城市选择
        form.on('select(city)', function (data) {
            if (!data.value) {
                $('select[name="area"]').html('');
                $('select[name="street"]').html('');
                form.render();
                return false;
            }
            //发异步，把数据提交给php
            $.ajax({
                type: 'get',
                url: '/base/getregion',
                data: { area_code: data.value, level: 3 },
                success: function (res) {
                    if (res.code == 200) {
                        var str = '<option value="">请选择区域</option>';
                        $.each(res.data, function (i, obj) {
                            str += '<option value="' + obj.area_code;
                            str += '">' + obj.name;
                            str += '</option>';
                        });
                        $('select[name="area"]').html(str);
                        $('select[name="street"]').html('');
                        form.render();
                    } else {
                        layer.msg(res.msg, { icon: 2, time: 1000 });
                    }
                }
            });
            return false;
        });
        //监听城市选择
        form.on('select(area)', function (data) {
            if (!data.value) {
                $('select[name="street"]').html('');
                form.render();
                return false;
            }
            //发异步，把数据提交给php
            $.ajax({
                type: 'get',
                url: '/base/getregion',
                data: { area_code: data.value, level: 4 },
                success: function (res) {
                    if (res.code == 200) {
                        var str = '<option value="">请选择搜索街道</option>';
                        $.each(res.data, function (i, obj) {
                            str += '<option value="' + obj.id;
                            str += '">' + obj.name;
                            str += '</option>';
                        });
                        $('select[name="street"]').html(str);
                        form.render();
                    } else {
                        layer.msg(res.msg, { icon: 2, time: 1000 });
                    }
                }
            });
            return false;
        });
        //监听代理
        form.on('select(agentid)', function (data) {
            $('select[name="cate"]').val('');
            $('select[name="subcate"]').html('');
            form.render();
            return false;
        });
        //监听分类
        form.on('select(cate)', function (data) {
            if (!data.value) {
                $('select[name="subcate"]').html('');
                form.render();
                return false;
            }
            var agentid = $('select[name="agentid"]').val();
            //发异步，把数据提交给php
            $.ajax({
                type: 'get',
                url: '/base/subcate',
                data: { cate: data.value, agentid: agentid },
                success: function (res) {
                    if (res.code == 200) {
                        var str = '<option value="">请选择子分类</option>';
                        $.each(res.data, function (i, obj) {
                            str += '<option value="' + obj.id;
                            str += '">' + obj.name;
                            if (obj.isshow == 2) {
                                str += '[已隐藏]';
                            }
                            str += '</option>';
                        });
                        $('select[name="subcate"]').html(str);
                        form.render();
                    } else {
                        layer.msg(res.msg, { icon: 2, time: 1000 });
                    }
                }
            });
            return false;
        });
    });
</script>